/***************************************************************************
 *   Copyright (C) 2008 by Łukasz Spintzyk   *
 *   lukasz.spintzyk@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef TIMER_H
#define TIMER_H

#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include "device_opt.h"
#include "display.h"
#include "adconversion.h"

#define TRIG_LEVEL DDATA_AREA_SIZE_H/4

/*! Function initializes timer1 necesarry for time base generators. It works in ctc mode, and generates interrupt on OCR1A match.
 */
void initTimer1();
register unsigned char time asm("r2");//!value indicates current time value

//!pointer on function which returns current x position of signal. It gives possibility of changing time generator
extern uint8_t (*getTime)();

/*! Gives us time base in shape of signal on channel 2.
 * adjustFreq-128 moves signal characteristic on to left or right,
 *  DDATA_AREA_SIZE/2 is half of screen, minus values means moving signal to left, plus values to right.
 * \return current x position on display.
 */
inline uint8_t getTimeFromCH2();

/*! Gives us saw shaped time base. Variable adjustFreq adds extra time to time base period(which won't be displayed) for callibration. 
 * \return current x position on display
 */
inline uint8_t getTimeFromTimer();

/*! The same function as getTimeFromTimer(), but with trigger level,
 * adjustFreq is used to indicate the trigger level.Values od adjustFreq from 0-127 rising edge, 128 falling edge.
 * This function works only with external adc(ch1).
 * \return current x position on display
 */
inline uint8_t getTimeFromTimerWithTriger();


/*! Function sets clock prescaller.
 * \param x value from 0-5, 0-clock stop, 1 - /1 ; 2 - /8 ; 3 - /64 ; 4 - /256 ; 5 - /1024
 */
inline void setPrescaller1(uint8_t x);

/*! Set time which timer1 will count in CTC mode 
 * \param t time to count in ms
 */
void setTime_ms(uint16_t t);

/*! Set time which timer1 will count in CTC mode 
 * \param t time to count in us
 */
void setTime_us(uint16_t t);


/*! Halts timer 1 
 */
void stopTimer1();


#endif
